(function ($) {

    var imageSet, platformData, acceptUrl, rejectUrl, SELECTED, id, status = 0;

    var layer = $('.js-image-set-viewer-selector');

    var mask = $.pageMask;

    var confirmForm = layer.find('.float-verify-con');
    var displayed = confirmForm.find('.js-displayed');
    var acceptBtn = confirmForm.find('.js-accept').hide();
    var rejectBtn = confirmForm.find('.js-reject');
    var closeBtn = confirmForm.find('.js-close');
    var platform = confirmForm.find('.js-platform');
    var statusDisplay = confirmForm.find('.js-status');

    var rejectForm = layer.find('.float-reject-con');
    var rejectCancelBtn = rejectForm.find('.js-reject-cancel');
    var rejectConfirmBtn = rejectForm.find('.js-reject-confirm');
    var rejectReason = rejectForm.find('.js-reject-reason');
    var rejectCloseBtn = rejectForm.find('.js-reject-close');

    var initStatus = 0;

    var acceptCallback = function () {
    };
    var rejectCallback = function () {
    };

    layer.on('click', '.js-switcher', function () {
        show_index(parseInt($(this).data('index')));
    });

    closeBtn.on('click', closeLayer);
    rejectCloseBtn.on('click', closeLayer);

    acceptBtn.on('click', function (e) {
        e.preventDefault();

        var data = {
            id: id,
            action: 'accept',
            selected: SELECTED
        };

        showConfirm('确认审核通过', 0, function () {
            $.post(acceptUrl, data, function (ret) {
                if ($.isFunction(acceptCallback) && ret) {
                    acceptCallback.call(null, ret);
                }
                if (ret) {
                    if (ret.flag == 0) {
                        showMsg('审批成功');
                        setTimeout(function () {
                            window.location.reload()
                        }, 500);
                    } else {
                        showMsg(ret.message);
                    }
                    return;
                }

                showMsg('系统错误, 请稍后重试');
            });
        });

        return false;
    });

    rejectBtn.on('click', function (e) {
        e.preventDefault();
        showRejectForm();
        return false;
    });

    displayed.on('click', function () {
        var el = $(this);
        platform.find('.js-switcher').removeClass('selected');
        if (el.hasClass('selected')) {
            el.removeClass('selected');
            SELECTED = null;
        } else {
            el.addClass('selected');
            SELECTED = 1 * el.data('index');
            platform.find('.js-switcher').eq(SELECTED).addClass('selected');
        }
        check_selected();
    });

    rejectCancelBtn.on('click', function (e) {
        e.preventDefault();
        showConfirmForm();
        return false;
    });

    rejectConfirmBtn.on('click', function (e) {
        e.preventDefault();

        var reason = $.trim(rejectReason.val());

        if (!reason) {
            rejectReason.effect('highlight');
            return false;
        }

        var data = {
            id: id,
            action: 'reject',
            reject_reason: reason
        };

        showConfirm('驳回', 0, function () {
            $.post(rejectUrl, data, function (ret) {
                // if ($.isFunction(acceptCallback) && ret) {
                //     rejectCallback.call(null, ret);
                // }
                if (ret) {
                    if (ret.flag == 0) {
                        showMsg('驳回成功');
                        setTimeout(function () {
                            window.location.reload()
                        }, 500);
                    } else {
                        showMsg(ret.message);
                    }
                    return;
                }

                showMsg('系统错误, 请稍后重试');
            });
        });
        return false;
    });

    function show_index(i) {

        confirmForm.find('.js-switcher').removeClass('focus');
        confirmForm.find('.js-switcher').eq(i).addClass('focus');

        displayed.removeClass('selected');

        if (SELECTED != null && SELECTED == i) {
            displayed.addClass('selected');
        }

        displayed.data('index', i);
        displayed.css({
            'background-image': 'url(' + imageSet[i] + ')'
        });
    }

    function check_selected() {
        if (SELECTED != null && SELECTED > -1) {
            acceptBtn.show();
        } else {
            acceptBtn.hide();
        }
    }

    function showLayer() {
        layer.show();
        mask.show();
    }

    function closeLayer() {
        layer.hide();
        mask.hide();
    }

    function showRejectForm() {
        layer.addClass('reject');
        rejectReason.val('');
        layer.children().hide();
        rejectForm.show();
    }

    function showConfirmForm() {
        layer.removeClass('reject');
        layer.children().hide();
        confirmForm.show();
    }

    $.fn.image_set_viewer_selector = function (options) {

        var images = $(this).find('img');

        if (images.length == 0) {
            return false;
        }

        if (options.id == null && options.status > 0) {
            return false;
        }

        id = options.id;
        status = options.status;
        if (status == 1) {
            statusDisplay.attr('class', 'js-status').addClass('accept').text('已通过');
        } else if (status == 2) {
            statusDisplay.attr('class', 'js-status').addClass('reject').text('已驳回');
        }

        initStatus = options.initStatus;
        rejectCloseBtn.hide();
        rejectCancelBtn.hide();
        if (initStatus == 2) {
            rejectCloseBtn.show();
            showRejectForm();
        } else {
            rejectCancelBtn.show();
            showConfirmForm();
        }


        acceptUrl = options.acceptUrl;
        acceptCallback = options.acceptCallback;
        rejectUrl = options.rejectUrl;
        rejectCallback = options.rejectCallback;

        SELECTED = null;
        imageSet = [];
        platformData = [];
        images.each(function (i) {
            var el = $(this);
            imageSet.push(el.attr('src'));
            platformData.push('<a data-index="' + i + '" class="js-switcher" href="javascript:void(0)"><img src="' + el.attr('src') + '"/></a>');
        });

        platform.html(platformData.join(''));

        show_index(0);

        var self = {
            show: showLayer,
            close: closeLayer
        };

        return self
    };
})(jQuery);